package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MouldCheckItemFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.MouldCheckItemMapper;
import com.iemes.mapper.mould.MouldCheckStandardMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/pBeforeCheckStandard/")
@Controller
public class PBeforeCheckStandardController extends BaseController {
	
	@Inject
	private MouldCheckStandardMapper mouldCheckStandardMapper;
	
	@Inject
	private MouldCheckItemMapper mouldCheckItemMapper;
	
	@RequestMapping("examineOrder")
	public String examineOrder(Model model){
		MouldCheckItemFormMap mouldCheckItemFormMap = getFormMap(MouldCheckItemFormMap.class);
		List<MouldCheckItemFormMap> item=mouldCheckItemMapper.findByWhere(mouldCheckItemFormMap);
		model.addAttribute("item",item);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/pBeforeCheckStandard/pCheckStandard_list";
	}
	
	@ResponseBody
	@RequestMapping("pCheckStandard_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
		//限定status为1
		mouldCheckStandardFormMap.put("status", 1);
		List<Map<String, Object>> mouldCheckStandardItems=mouldCheckStandardMapper.getAllItemBy1(mouldCheckStandardFormMap);
		LuogicPaging(pageNow, pageSize, mouldCheckStandardItems);
		return pageView;
	}
	
	@RequestMapping("pCheckStandard_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		//获取所有检查项目
		List<MouldCheckItemFormMap> mouldCheckItemFormMaps=mouldCheckItemMapper.findByWhere(new MouldCheckItemFormMap());
		model.addAttribute("item",mouldCheckItemFormMaps);
		if(Common.isNotEmpty(id)){
			MouldCheckStandardFormMap mouldCheckStandardFormMap=mouldCheckStandardMapper.findbyFrist("id", id, MouldCheckStandardFormMap.class);
			model.addAttribute("pCheckStandard",mouldCheckStandardFormMap);
		}
		return Common.BACKGROUND_PATH + "/mould/pBeforeCheckStandard/pCheckStandard_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="pCheckStandard_editEntity")
	@SystemLog(module="备模前模具质量检查",methods="编辑检查标准-修改检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		mouldCheckStandardFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		mouldCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		mouldCheckStandardMapper.editEntity(mouldCheckStandardFormMap);
		return "success";
	}
	
	@RequestMapping("pCheckStandard_add")
	public String addUI(Model model) throws Exception {
		//获取所有检查项目
		List<MouldCheckItemFormMap> mouldCheckItemFormMaps=mouldCheckItemMapper.findByWhere(new MouldCheckItemFormMap());
		model.addAttribute("item",mouldCheckItemFormMaps);
		return Common.BACKGROUND_PATH + "/mould/pBeforeCheckStandard/pCheckStandard_add";
	}
	
	@ResponseBody
	@RequestMapping("pCheckStandard_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="备模前模具质量检查",methods="编辑检查标准-新增检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		mouldCheckStandardFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		mouldCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		mouldCheckStandardMapper.addEntity(mouldCheckStandardFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("pCheckStandard_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="备模前模具质量检查",methods="编辑检查标准-删除检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		MouldCheckStandardFormMap mouldCheckStandardFormMap=getFormMap(MouldCheckStandardFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		for(int i=0;i<str.length;i++){
			mouldCheckStandardFormMap.put("date", sdf.format(date));
			mouldCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
			mouldCheckStandardFormMap.set("status", 0);
			mouldCheckStandardFormMap.put("id", str[i]);
			mouldCheckStandardMapper.editEntity(mouldCheckStandardFormMap);
		}
		return "success";
	}
}








